﻿using HARTCalibrationTool.Commands.BatchCalibrationBoard;
using HARTCalibrationTool.SerialPortManager;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool.Commands.HART
{
    public class HART40EnterExitFixedCurrentMode : HARTCMDsBase
    {
        private float _current = 0.0f;

        public HART40EnterExitFixedCurrentMode(SerialPortsBase serialPortManager, string commandName, float current) : base(serialPortManager, commandName)
        {
            _stHARTCMDsStruct = new HARTCMDsCodeStruct()
            {
                preamble = new byte[5] { 0xff, 0xff, 0xff, 0xff, 0xff },
                start = 0x82,
                addressByte1 = 0xA2,
                addressByte2 = 0x82,
                addressByte3 = 0x74,
                addressByte4 = 0x2E,
                addressByte5 = 0x96,
                cmdID = 0x28,
                size = 0x04,
                parity = 0xFF,
            };

            _current = current;
        }

        protected override async Task SendCMD()
        {
            byte[] deviceID = GetDeviceID();
            byte[] cmdCode = null;
            byte[] tempData = new byte[32];
            List<byte> cmdBytesList = new List<byte>();
            byte[] currentBytes = null;

            if ((deviceID[0] == 0x00) && (deviceID[1] == 0x00) && (deviceID[2] == 0x00))
            {
                SerialPortsBase.PrintMessage("通道" + Utils.CountTrailingZeros((uint)GetCMDsChannelID()).ToString() + ", Device ID 错误！");
                return;
            }

            _stHARTCMDsStruct.addressByte3 = deviceID[0];
            _stHARTCMDsStruct.addressByte4 = deviceID[1];
            _stHARTCMDsStruct.addressByte5 = deviceID[2];

            currentBytes = BitConverter.GetBytes(_current);
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(currentBytes);
            }

            cmdCode = StructToBytes<HARTCMDsCodeStruct>(_stHARTCMDsStruct);

            cmdBytesList.AddRange(cmdCode.Skip(0).Take(13));
            cmdBytesList.AddRange(currentBytes);
            Array.Clear(tempData, 0, tempData.Length);
            cmdBytesList.CopyTo(5, tempData, 0, 12);
            cmdBytesList.Add(Utils.GetHARTCRC(tempData, 12));

            SerialPortsBase.PrintMessage("--> " + Utils.HexToString(cmdBytesList.ToArray()));

            await SerialPortMager.SerialPortWrite(cmdBytesList.ToArray());
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() =>
            {
                SerialPortsBase.PrintMessage("<-- " + $"{Utils.HexToString(data)}");

                SerialPortMager.NotifyObserver(HARTObserversInfo.info["HART #40 EE Fixed Current Mode"], null);
            });
        }

        public override void OnFailure()
        {

        }

        public override void OnSuccess()
        {

        }

        public override void OnTimeout()
        {

        }
    }
}
